package com.xh.demo.service;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.demo.datasource.annotation.LakeDataSource;
import com.xh.demo.datasource.enums.DataSourceEnum;
import com.xh.demo.mapper.UserMapper;
import com.xh.demo.model.table.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wen
 * @since 2021-08-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    @LakeDataSource(DataSourceEnum.MASTER)
    public User getUserByMaster(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    @LakeDataSource(DataSourceEnum.OTHER)
    public User getUserByOther(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    @LakeDataSource(DataSourceEnum.MASTER)
    public User saveByUserMaster(User user) {
        user.setId(IdWorker.getId());
        this.baseMapper.insert(user);
        return user;
    }

    @Override
    @LakeDataSource(DataSourceEnum.OTHER)
    public User saveByUserOther(User user) {
        user.setId(IdWorker.getId());
        this.baseMapper.insert(user);
        return user;
    }

    @Override
    @Transactional
    @LakeDataSource(DataSourceEnum.MASTER)
    public User saveByUserMasterTransactional(User user) {
        user.setId(IdWorker.getId());
        this.baseMapper.insert(user);
        throw new RuntimeException();
    }

    @Override
    @Transactional
    @LakeDataSource(DataSourceEnum.OTHER)
    public User saveByUserOtherTransactional(User user) {
        user.setId(IdWorker.getId());
        this.baseMapper.insert(user);
        throw new RuntimeException();
    }

}
